/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.tree.cellRenderer;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.tree.TreeCellRenderer;

import org.jxSwing.component.tree.node.CheckNode;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.tree.cellRenderer
 * @date 15/02/2014 - 08:47:16
 */
public class CheckRenderer extends JPanel implements TreeCellRenderer {
	
	private static final long serialVersionUID = 2001590399202098427L;
	
	protected JCheckBox check;
	
	protected TreeLabel label;
	
	/**
	 * 
	 */
	public CheckRenderer() {
	
		setLayout(null);
		
		add(check = new JCheckBox());
		add(label = new TreeLabel());
		
		check.setBackground(UIManager.getColor("Tree.textBackground"));
		label.setForeground(UIManager.getColor("Tree.textForeground"));
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Container#doLayout()
	 */
	@Override
	public void doLayout() {
	
		final Dimension d_check = check.getPreferredSize();
		final Dimension d_label = label.getPreferredSize();
		
		int y_check = 0;
		int y_label = 0;
		
		//
		if(d_check.height < d_label.height) {
			
			y_check = (d_label.height - d_check.height) / 2;
			
		}else {
			
			y_label = (d_check.height - d_label.height) / 2;
		}
		
		check.setLocation(0, y_check);
		check.setBounds(0, y_check, d_check.width, d_check.height);
		
		label.setLocation(d_check.width, y_label);
		label.setBounds(d_check.width, y_label, d_label.width, d_label.height);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JComponent#getPreferredSize()
	 */
	@Override
	public Dimension getPreferredSize() {
	
		final Dimension d_check = check.getPreferredSize();
		final Dimension d_label = label.getPreferredSize();
		
		//
		return new Dimension(d_check.width + d_label.width, d_check.height < d_label.height ? d_label.height : d_check.height);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree, java.lang.Object, boolean, boolean, boolean, int, boolean)
	 */
	@Override
	public Component getTreeCellRendererComponent(final JTree tree, final Object value, final boolean isSelected, final boolean expanded, final boolean leaf, final int row, final boolean hasFocus) {
	
		final String stringValue = tree.convertValueToText(value, isSelected, expanded, leaf, row, hasFocus);
		
		setEnabled(tree.isEnabled());
		
		check.setSelected(((CheckNode)value).isSelected());
		
		label.setFont(tree.getFont());
		label.setText(stringValue);
		label.setSelected(isSelected);
		label.setFocus(hasFocus);
		
		//
		if(leaf) {
			
			label.setIcon(UIManager.getIcon("Tree.leafIcon"));
			
		}else if(expanded) {
			
			label.setIcon(UIManager.getIcon("Tree.openIcon"));
			
		}else {
			
			label.setIcon(UIManager.getIcon("Tree.closedIcon"));
		}
		
		return this;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JComponent#setBackground(java.awt.Color)
	 */
	@Override
	public void setBackground(Color color) {
	
		//
		if(color instanceof ColorUIResource) {
			
			color = null;
		}
		
		super.setBackground(color);
	}
	
	/**
	 * @author Denilson Edinaldo Pinto
	 * @porject jxSwing
	 * @package org.jxSwing.component.tree.cellRenderer
	 * @date 15/02/2014 - 09:10:34
	 */
	public class TreeLabel extends JLabel {
		
		private static final long serialVersionUID = 6444542608521274098L;
		
		boolean hasFocus;
		
		boolean isSelected;
		
		/**
		 * 
		 */
		public TreeLabel() {
		
		}
		
		/*
		 * (non-Javadoc)
		 * 
		 * @see javax.swing.JComponent#getPreferredSize()
		 */
		@Override
		public Dimension getPreferredSize() {
		
			Dimension retDimension = super.getPreferredSize();
			
			//
			if(retDimension != null) {
				
				retDimension = new Dimension(retDimension.width + 3, retDimension.height);
			}
			
			return retDimension;
		}
		
		/*
		 * (non-Javadoc)
		 * 
		 * @see javax.swing.JComponent#paint(java.awt.Graphics)
		 */
		@Override
		public void paint(final Graphics graphics) {
		
			String text;
			
			//
			if((text = getText()) != null) {
				
				//
				if(0 < text.length()) {
					
					//
					if(isSelected) {
						
						graphics.setColor(UIManager.getColor("Tree.selectionBackground"));
						
					}else {
						
						graphics.setColor(UIManager.getColor("Tree.textBackground"));
					}
					
					//
					final Dimension d = getPreferredSize();
					
					int imageOffset = 0;
					
					final Icon currentI = getIcon();
					
					//
					if(currentI != null) {
						
						imageOffset = currentI.getIconWidth() + Math.max(0, getIconTextGap() - 1);
					}
					
					//
					graphics.fillRect(imageOffset, 0, d.width - 1 - imageOffset, d.height);
					
					//
					if(hasFocus) {
						
						graphics.setColor(UIManager.getColor("Tree.selectionBorderColor"));
						graphics.drawRect(imageOffset, 0, d.width - 1 - imageOffset, d.height - 1);
					}
				}
			}
			
			super.paint(graphics);
		}
		
		/*
		 * (non-Javadoc)
		 * 
		 * @see javax.swing.JComponent#setBackground(java.awt.Color)
		 */
		@Override
		public void setBackground(Color color) {
		
			//
			if(color instanceof ColorUIResource) {
				
				color = null;
			}
			
			super.setBackground(color);
		}
		
		/**
		 * @param hasFocus
		 */
		public void setFocus(final boolean hasFocus) {
		
			this.hasFocus = hasFocus;
		}
		
		/**
		 * @param isSelected
		 */
		public void setSelected(final boolean isSelected) {
		
			this.isSelected = isSelected;
		}
	}
}
